package com.lance.answer.question_export;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.dto.Item;
import com.lance.answer.gui.answer.Question;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.util.MD5Utils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.FileInputStream;
import java.time.Duration;
import java.util.*;

/**
 * <p>以老师的身份获取答案<p>
 *
 * @author : fengx
 * @since JDK8.0
 * <p>
 * <p>
 * 后面
 * 选择题、单元自测题、计分、分、自测、综合练习 都要获取
 * create by fengx on 2021/7/15 14:47
 */
public class GetAllCourse {

    public static void login(WebDriver driver, String userName, String pwd) {
        WebElement agreeButton = driver.findElement(By.id("RememberLogin"));
        agreeButton.click();

        WebElement userNameInput = driver.findElement(By.id("username"));
        userNameInput.clear();
        userNameInput.sendKeys(userName);

        WebElement passwordInput = driver.findElement(By.id("password"));
        passwordInput.clear();
        passwordInput.sendKeys(pwd);


        WebElement loginBtn = driver.findElement(By.name("button"));

        loginBtn.click();
    }

    public static void main(String[] args) throws Exception {
        ChromeDriver driver = Util.getDriver(null);
        String url = "http://thome.ouchn.cn/";
        driver.get(url);
        Thread.sleep(10000);
//        String pwd = "zsxy123456WY";
        String userName = "zsxypy_1";
        String pwd = "zsxypy_1";

        // start 2022-6-12 19:10
        // 统计学原理


        login(driver, userName, pwd);

        Thread.sleep(1000);
        Util.switchToFirst(driver);

        // d://newAnswer//

        //   for (String id : SUBJECT.keySet()) {


        driver.get("http://thome.ouchn.cn/");
        Thread.sleep(2000);
        driver.findElement(By.id("txtclassname")).clear();
        Thread.sleep(2000);


        Set<String> courseSet = Sets.newHashSet();

        int i = 18;

        while (i>0){


            for (WebElement tr : new WebDriverWait(driver, Duration.ofSeconds(20)).until((ExpectedCondition<List<WebElement>>)
                    d -> d.findElements(By.tagName("tr")))) {


                List<WebElement> tdWeList = tr.findElements(By.tagName("td"));

                if(tdWeList.size() != 5){
                    continue;
                }

                courseSet.add(tdWeList.get(1).getText().trim());

                System.err.println(tr.getText());

            }


            for(WebElement aWe:driver.findElements(By.tagName("a"))){
                if("下一页".equals(aWe.getText())){
                    aWe.click();
                    break;
                }
            }
            Thread.sleep(5000);
            i--;
        }

        for(String course:courseSet){
            System.err.println("SUBJECT.put(\""+course+"\",\""+course+"\");");
        }

        Thread.sleep(5000);


    }
}
